package com.itcloud.test;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory ;
import org.junit.Test;

public class ShiroLogin {
    //进行Shiro认证登录
    @Test
    public void testShiroLongin(){
        //加载shiro.ini的配置文件
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-config/shiroRealm.ini") ;
        //创建安全管理者
        SecurityManager securityManager = factory.getInstance();
        //设置当前的环境,安全管理者
        SecurityUtils.setSecurityManager(securityManager);
        //创建认证主体
        Subject subject = SecurityUtils.getSubject() ;//模拟主体登录，构建一个subject
        //这里的参数username 和password来自用户输入
        UsernamePasswordToken token = new UsernamePasswordToken("root","admin") ;
        try { //进行异常捕获，有可能密码或者用户名错误
            subject.login(token); //token会传入MyRealm方法doGetAuthenticationInfo中
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("用户是否登录成功"+subject.isAuthenticated());
        subject.logout(); //登出
        System.out.println("用户是否登录成功"+subject.isAuthenticated());

    }
    @Test
    public void Md5Password(){
        String originalPassword = "admin" ;
        String md2Password = new Md5Hash(originalPassword).toString() ;
        System.out.println("MD5加密后的密码"+md2Password);
        //进行盐值加密
        String saltPassword = new Md5Hash(originalPassword,"root").toString() ;//这里使用用户密码和用户名进行盐值加密
        System.out.println("盐值加密后的密码"+saltPassword);

        //进行多次盐值加密
        String MoreSaltPassword = new Md5Hash(originalPassword,"root",10).toString() ; //进行十次撒盐加密
        System.out.println("多次盐值加密后的密码"+MoreSaltPassword);
    }
}
